package com.neusoft.neuiotms.dcom.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.file.dto.FileDTO;
import com.neusoft.bizcore.file.service.FileService;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.dto.KnowledgeDTO;
import com.neusoft.neuiotms.dcom.service.KnowledgeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "知识库管理")
@RequestMapping(value = "/api/w/knowledge")
@RestController
public class KnowledgeController {

    @Autowired
    private KnowledgeService knowledgeService;

    @Autowired
    private FileService fileService;

    @ApiOperation("查询所有知识")
    @GetMapping
    public ResultListDTO<KnowledgeDTO> index(final Searchable searchable) {
        return ResultListDTO.success(this.knowledgeService.index(searchable));
    }

    @ApiOperation("分页查询所有知识")
    @GetMapping("/p")
    public PageResultDTO<KnowledgeDTO> page(final Searchable searchable, final Pageable pageable) {
        return PageResultDTO.success(this.knowledgeService.page(searchable, pageable));
    }

    @ApiOperation("新增知识")
    @PostMapping
    public ResultDTO<KnowledgeDTO> create(@RequestBody final KnowledgeDTO dto, final UserBean user) {
        return ResultDTO.success(this.knowledgeService.create(dto, user));
    }

    @ApiOperation("更新知识")
    @PutMapping("/{id}")
    public ResultDTO<KnowledgeDTO> update(@PathVariable final Long id, @RequestBody final KnowledgeDTO dto,
            final UserBean user) {
        return ResultDTO.success(this.knowledgeService.update(id, dto, user));
    }

    @ApiOperation("查看知识")
    @GetMapping("/{id}")
    public ResultDTO<KnowledgeDTO> detail(@PathVariable final Long id) {
        return ResultDTO.success(this.knowledgeService.detail(id));
    }

    @ApiOperation("更新知识浏览量")
    @PutMapping("/update/volume/{id}")
    public ResultDTO<Void> updateReadingVolume(@PathVariable final Long id) {
        this.knowledgeService.updateReadingVolume(id);
        return ResultDTO.success();
    }

    @ApiOperation("删除知识")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.knowledgeService.delete(id);
        return ResultDTO.success();
    }

    @ApiOperation("上传文件")
    @PostMapping("/attachment")
    public ResultDTO<FileDTO> upload(@RequestParam("file") final MultipartFile file) {
        final FileDTO doc = this.fileService.save(file);
        return ResultDTO.success(doc);
    }

    @ApiOperation("下载文件")
    @GetMapping("/export/{fsId}")
    public void download(@PathVariable final String fsId, final HttpServletResponse response) {
        this.fileService.download(fsId, response);
    }

    @ApiOperation("删除文件")
    @DeleteMapping("/attachment/{fsId}/{id}")
    public void download(@PathVariable final String fsId, @PathVariable final Long id) {
        this.knowledgeService.deleteMongoFile(fsId, id);
    }
}
